//
// Created by liweimin on 2021/7/2.
//
#include "../header/stack/stack.h"
#include <ctype.h>

bool in(char c, char op[])
{
    char *p;
    p = op;
    while (*p != '\0')
    {
        if (c == *p)
        {
            return true;
        }
        p++;
    }
    return false;
}

char Precede(char a, char b)
{
    int i, j;
    char pre[][7] = {
            /*运算符之间的优先级制作成一张表格*/
            {'>', '>', '<', '<', '<', '>', '>'},
            {'>', '>', '<', '<', '<', '>', '>'},
            {'>', '>', '>', '>', '<', '>', '>'},
            {'>', '>', '>', '>', '<', '>', '>'},
            {'<', '<', '<', '<', '<', '=', '0'},
            {'>', '>', '>', '>', '0', '>', '>'},
            {'<', '<', '<', '<', '<', '0', '='}};
    switch (a)
    {
        case '+':
            i = 0;
            break;
        case '-':
            i = 1;
            break;
        case '*':
            i = 2;
            break;
        case '/':
            i = 3;
            break;
        case '(':
            i = 4;
            break;
        case ')':
            i = 5;
            break;
        case '#':
            i = 6;
            break;
    }
    switch (b)
    {
        case '+':
            j = 0;
            break;
        case '-':
            j = 1;
            break;
        case '*':
            j = 2;
            break;
        case '/':
            j = 3;
            break;
        case '(':
            j = 4;
            break;
        case ')':
            j = 5;
            break;
        case '#':
            j = 6;
            break;
    }
    return pre[i][j];
}


int Operate(int a, char theta, int b)
{
    int i, j, result;
    i = a;
    j = b;

    switch (theta)
    {
        case '+':
            result = i + j;
            break;
        case '-':
            result = i - j;
            break;
        case '*':
            result = i * j;
            break;
        case '/':
            result = i / j;
            break;
    }
    return result;
}

int getNext(int *n)
{
    char c;
    *n = 0;
    while ((c = getchar()) == ' ');
    if (!isdigit(c))
    {
        *n = c;
        return 1;
    }

    do
    {
        *n = *n * 10 + (c - '0');
        c = getchar();
    } while (isdigit(c));
    ungetc(c, stdin);
    return 0;
}

int EvaluateExpression()
{
    int n;
    int flag;
    int c = 0;
    char x, theta;
    int a, b;

    char OP[] = "+-*/()#";
    SqStack OPTR;
    SqStack OPND;
    init(&OPTR);
    push(&OPTR, '#');
    init(&OPND);
    flag = getNext(&c);
    getTop(&OPTR, &x);
    while (c != '#' || x != '#')
    {
        if (flag == 0)
        {
            push(&OPND, c);
            flag = getNext(&c);
        }
        else
        {
            getTop(&OPTR, &x);
            switch (Precede(x, c))
            {
                case '<'://栈顶元素优先级低
                    push(&OPTR, c);
                    flag = getNext(&c);
                    break;
                case '='://脱括号并接受下一字符
                    pop(&OPTR, &x);
                    flag = getNext(&c);
                    break;
                case '>':// 退栈并将运算结果入栈
                    pop(&OPTR, &theta);
                    pop(&OPND, &b);
                    pop(&OPND, &a);
                    push(&OPND, Operate(a, theta, b));
                    break;
            }
        }
        getTop(&OPTR, &x);
    }
    getTop(&OPND, &c);
    return c;
}

int main()
{
    int c;
    printf("Please input one expression:");
    c = EvaluateExpression();
    printf("Result=%d\n", c);
//    getch();
    return 0;
}
